package cn.tedu.sp04.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import pojo.Item;
import pojo.Order;
import pojo.User;
import service.OrderService;
import web.util.JsonResult;

import java.util.Arrays;

@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    public JsonResult<Order> getOrder(@PathVariable("orderId") String orderId) {
        log.info("获取订单，订单号：{}", orderId);
        Order order = orderService.getOrder(orderId);
        return JsonResult.ok(order);
    }

    @PostMapping("/")
    public JsonResult<?> addOrder() {
        // 模拟添加订单
        Order order = new Order();
        order.setId("123");
        order.setUser(new User(1, "新订单用户", "123321"));
        order.setItems(Arrays.asList(new Item[]{
                new Item(1, "手机", 1),
                new Item(2, "电脑", 2),
                new Item(3, "显示器", 3),
                new Item(4, "投影仪", 4),
                new Item(5, "音箱", 5),
        }));
        orderService.addOrder(order);
        return JsonResult.ok();
    }
}
